//
//  ZZmessageController.m
//  pacc
//
//  Created by 测试 on 2018/6/8.
//  Copyright © 2018年 测试. All rights reserved.
//

#import "ZZmessageController.h"
#import "messageModel.h"
#import "messageCell.h"
#import "taskDetailsCVc.h"
#import "shenhetuihuiController.h"
@interface ZZmessageController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,copy)NSMutableArray * dataSource;
@property(nonatomic,strong)UITableView * tableView;
@end

@implementation ZZmessageController

- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    tableView.backgroundColor = baseBackgroundColor;
    [tableView registerNib:[UINib nibWithNibName:@"messageCell" bundle:nil] forCellReuseIdentifier:@"message"];
    tableView.delegate = self;
    tableView.dataSource = self;
    _tableView = tableView;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    [self loadData];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
}
-(void)viewWillAppear:(BOOL)animated{
    [self loadData];
}
-(void)loadData{
    NSMutableDictionary *dic =[NSMutableDictionary dictionary];
    NSString *str =[NSString stringWithFormat:@"msgmsgList"];
    NSString *nameStr =@"消息";
    [dic setValue:@1000 forKey:@"size"];
    [dic setValue:@1 forKey:@"page"];
    [dic setValue:@"医生端" forKey:@"project"];
    [dic setValue:my_AppDelegate.token forKey:@"token"];
    
    [NetWorking startPOSTRequestWithUrl:str parameters:dic logMes:nameStr show:NO success:^(NSDictionary *data) {
        NSArray *arr = [data objectForKey:@"data"];
        self->_dataSource = [messageModel modelSformDics:arr];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self->_tableView reloadData];
            [self->_tableView.mj_header endRefreshing];
        });
    } failure:^(NSError *error) {
        
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    messageCell * cell = [tableView dequeueReusableCellWithIdentifier:@"message"];
    cell.arrData = _dataSource[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    messageModel * model = _dataSource[indexPath.row];
    int height =[model.content stringSizeWithMaxSize:CGSizeMake(ScreenWidth-30, ScreenHeight) withFont:textfont].height;
    return height;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    messageCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *urlType = cell.urlType;
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    [param setValue:cell.msgid forKey:@"msgId"];
    [param setValue:my_AppDelegate.token forKey:@"token"];
    [NetWorking startPOSTRequestWithUrl:@"msgreadMsg" parameters:param logMes:@"消息已读" show:NO success:^(NSDictionary *data) {
        
    } failure:^(NSError *error) {
        
    }];
    if ([cell.messageLabel.text containsString:@"未通过"]) {
        shenhetuihuiController * vc = [shenhetuihuiController new];
        vc.zid = cell.zid;
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    taskDetailsCVc *vc =[taskDetailsCVc new];
    vc.zid = cell.zid;
    NSString * nameStr;
    //    urlType    ORDER_DETAIL        REPORT_DETAIL        REVIEW_DETAIL        ORDER_LIST        REPORT_LIST        REVIEW_LIST
    //                   工单详情            报告详情                审核详情            工单列表            报告列表
    //    审核列表
    
    if ([urlType isEqualToString:@"WRITE_DETAIL"]) {
        //        医生端申请单详情页，有保存和提交

    }else if ([urlType isEqualToString:@"AUDIT_DETAIL"]){
        //        医生端申请单详情页，有不通过和通过
            vc.type = 8;
    }else if ([urlType isEqualToString:@"REPORT_DETAIL"]){
        //        医生端申请单详情页，没有按钮，不能编辑
        
        
    }else if ([urlType isEqualToString:@"ASSIGN_LIST"]){
        //        派单列表
        my_AppDelegate.fromMessage = YES;
        self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:1];
        return;
    }else if ([urlType isEqualToString:@"ORDER_DETAIL"]){
        //        用户端申请单详情页
        
    }
    else{
        return;
    }
    vc.odrStatus = nameStr;
    [self.navigationController pushViewController:vc animated:YES];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
